<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        background-color: rgb(118, 211, 219);
      }
    </style>
  </head>
  <body>
    <button>开始</button>
    <div></div>
    <script>
      window.onload = function () {
        var btn = document.querySelector("button");
        var div = document.querySelector("div");
        var begin = 0,
          step = 10,
          end = 400;
        timer = null;
        btn.addEventListener("click", function () {
          timer = setInterval(function () {
            begin += step;
            div.style.marginLeft = begin + "px";
            if (begin >= end) {
              clearInterval(timer);
            }
          }, 10);
        });
      };
    </script>
  </body>
</html>
